import { http } from '@/plugins/axios'
import useUtils from '@/utils'

const { encodeSearchParams } = useUtils()
export default () => {
    const index = async (page: number = 0) => {
        return await http.request<any>({
            url: `department`,
            method: "GET"
        })
    }
    // 新增
    const store = async (data: DepartmentModel) => {
        return await http.request<any>({
            url: `department`,
            method: 'POST',
            data: data
        })
    }
    const update = async (data: DepartmentModel) => {
        return await http.request<any>({
            url: `department/${data.id}`,
            method: "PUT",
            data: data
        })
    }
    const destory = async (id: number) => {
        return await http.request<any>({
            url: `department/${id}`,
            method: "DELETE",
        })
    }

    const deptUsers = async (id: number,page: number = 0, obj: Object) => {
        let data = encodeSearchParams(obj)
        return await http.request<any>({
            url: `department/${id}/users?page=${page}&${data}`,
            method: "GET"
        })
    }
    // 设置管理员
    const setManager = async (id: number, user_id: number) => {
        return await http.request<any>({
            url: `department/${id}/bindManager`,
            method: "POST",
            data: { user_id: user_id }
        })
    }


    // 解除管理员
    const unSetManager = async (id: number, user_id: number) => {
        return await http.request<any>({
            url: `department/${id}/unBindManager`,
            method: "POST",
            data: { user_id: user_id }
        })
    }


    return {
        index,
        store,
        update,
        destory,
        deptUsers,
        setManager,
        unSetManager
    }
}